/**
 * @param {string} s
 * @return {string}
 */
var longestPalindrome = function (s) {
  let currentStrIndex = 0
  const maxLen = s.length - 1
  let left = 0
  let right = 0
  let resStr = s.charAt(currentStrIndex)
  let maxStr = s.charAt(currentStrIndex)
  let currentStr = s.charAt(currentStrIndex)
  let all = true
  let time = 0
  while (currentStrIndex !== maxLen) {
    currentStr = right === left ? currentStr + s.charAt(currentStrIndex + ++right) : s.charAt(currentStrIndex - ++left) + currentStr
    if (!all) {
      maxStr = maxStr.length >= resStr.length ? maxStr : resStr
      currentStrIndex++
      resStr = s.charAt(currentStrIndex)
      right = 0
      left = 0
      time = 0
      all = true
      currentStr = s.charAt(currentStrIndex)
    } else if (s.charAt(currentStrIndex - left) && s.charAt(currentStrIndex + right)) {
      if (currentStr.length > maxStr.length) {
        time++
        if (currentStr.split('').reverse().join('') === currentStr) {
          time = 0
          resStr = currentStr
        } else {
          time > 2 && (all = false)
        }
      }
    } else {
      maxStr = maxStr.length >= resStr.length ? maxStr : resStr
      currentStrIndex++
      resStr = s.charAt(currentStrIndex)
      right = 0
      left = 0
      time = 0
      all = true
      currentStr = s.charAt(currentStrIndex)
    }
  }
  return maxStr
}
console.log(longestPalindrome('abcba'))
